package me.fung.rem.app.content

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import me.fung.keen.app.view.View
import me.fung.rem.app.content.card.rememberItemCard
import me.fung.rem.app.content.card.addCard
import me.fung.rem.bean.repo.configUseAddCard
import me.fung.rem.component.CellLayout

const val PAGE_REM_CONTENT = "PAGE_CONTENT"

class ContentView : View<ContentPresenter>() {

    @Composable
    override fun content(modifier: Modifier) {
        val list = remember { p.list }
        val rp = remember { p }
        LaunchedEffect(Unit) {
            p.deleteSearch()
        }
        LaunchedEffect(rp.filter.value, rp.filterList.toList()) {
            p.load()
        }
        Box(modifier = modifier) {
            topBar(modifier = Modifier.align(Alignment.TopCenter).height(30.dp))
            buttonSetting(Modifier.align(Alignment.TopEnd).zIndex(10f).padding(end = 10.dp))
            Box(modifier = Modifier.padding(top = 40.dp)) {
                val scrollState = rememberScrollState()
                CellLayout(
                    modifier = Modifier.verticalScroll(scrollState),
                ) {
                    list.forEach {
                        rememberItemCard(it)
                    }
                    if (configUseAddCard) {
                        addCard()
                    }

                }
                VerticalScrollbar(ScrollbarAdapter(scrollState), modifier = Modifier.align(Alignment.CenterEnd))
            }
            if (!configUseAddCard) {
                buttonAdd(modifier = Modifier.align(Alignment.BottomEnd))
            }
        }
    }
}
